/**
 * Created by wuye on 2017/12/11.
 */
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/withdraw/authorList',
        datatype: "json",
        colModel: [
            { label: 'ID', name: 'id', index: "id", width: 45, key: true },
            { label: '申请人', name: 'applyname', width: 90 },
            { label: '联系方式', name: 'applyphone', width: 90 },
            { label: '申请金额', name: 'money', width: 75 ,formatter:function (value,options,row) {
                        return value/100+'元'
                }},
            { label: '剩余蝉币', name: 'balance', width: 80 ,formatter:function (value,options,row) {
                    return value/100
                }},
            { label: '申请状态', name: 'status', index: 'status', width: 80,formatter: function(value, options, row){
                    if(value === 0){
                        return '<span class="label label-primary">已申请</span>';
                    }
                    if(value === 1){
                        return '<span class="label label-success">已通过</span>';
                    }
                    if(value === 2){
                        return '<span class="label label-warning">已拒绝</span>';
                    }
                    if(value === 3){
                        return '<span class="label label-info">已打款</span>';
                    }
                }  },
            { label: '时间', name: 'createdate', index: "createuser", width: 90},
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            bookname: null
        },
        showList: true,
        title:null,
        // roleList:{},
        banner:{
            bannerpic:null
        },
        withdraw: {},
        q:{
            applyphone:null
        },
        sels: [
            {value:"0",week:"已申请"},
            {value:"1",week:"已通过"},
            {value:"2",week:"已拒绝"},
            {value:"3",week:"已打款"}
        ],
        bankCard:{
            phone:''
        },
        userinfo:{},
        mobile:null,
        code:null,
        money:null,
        bankCardNum:'',
        count:0,
        codeWord:'获取验证码',
    },
    created:function () {
        $.ajax({
            url: baseURL + 'sys/user/userInfo',
            type: "POST",
            contentType: "application/json",
            success: function(result) {
                if (result.code===0){
                    vm.userinfo=result.user
                    console.log(result)
                    vm.getBankCard()
                } else {
                    vm.userinfo={}
                    alert(result.msg)
                }
            },
            error: function(error) {
                alert('数据加载失败!');
            }
        })
        this.getBankCard()
    },
    methods: {
        getBankCard:function(){
            $.ajax({
                url: baseURL + 'sys/bankcard/getBankCard',
                type: "POST",
                contentType: "application/json",
                success: function(result) {
                    console.log(result)
                    if(result.bankcard){
                        vm.bankCard=result.bankcard;
                        vm.bankCardNum=result.bankcard.bankcard.substring(0,3)+'****'+result.bankcard.bankcard.substring(result.bankcard.bankcard.length-4,result.bankcard.bankcard.length)+'('+result.bankcard.bankname+")"
                    }else {

                    }
                },
                error: function(error) {
                    alert('数据加载失败!');
                }
            })
        },
        getCode:function(){
            if (vm.bankCard.phone) {
                var THIS = this;
                var obj = {
                    phone: THIS.bankCard.phone
                };
                $.ajax({
                    url:  '/youxiang/api/getCode?'+'phone='+obj.phone,
                    type: "GET",
                    contentType: "application/json",
                    success: function(result) {
                        if(result.code==0){
                            vm.counting()
                            alert('发送成功，请注意查收短信')
                        }else {
                            alert('发送失败，请稍后重试')
                        }
                    },
                    error: function(error) {
                        alert('数据加载失败!');
                    }
                });
            } else {
                alert("请输入正确格式手机号");
            }
        },
        // 倒计时
        counting:function() {
            setTimeout(function(){
                vm.count=60;
                var counting=setInterval(function () {
                    vm.count=vm.count-1;
                    vm.codeWord=vm.count+"s秒后重发";
                    if(!vm.count){
                        clearInterval(counting);
                        vm.codeWord='获取验证码';
                        vm.count=0;
                    }
                },1000)
            },500)
        },
        // 查看拒绝原因
        reason:function(){
            var id=getSelectedRows();
            if(id.length){
                if(id.length==1){
                        $.get(baseURL + "sys/withdraw/reason?id="+id[0], function(r){
                            if(r.code==500){
                                alert(r.msg);
                            }else {
                                alert('拒绝原因：'+r.withdraw.refusereason);
                            }
                        });
                }else {
                    alert('只能选择一条记录')
                }

            }else {
                alert('请选择一条记录')
            }

        },
        changeImage: function(e) {
            var file = e.target.files[0]
            var reader = new FileReader()
            var THIS = this
            reader.readAsDataURL(file)
            reader.onload = function(e) {
                // this.result 为图片
                THIS.upload(this.result)
            }
        },
        upload: function(data) {
            // 上传图片
            var THIS = this;
            if(this.$refs.file.files.length !== 0) {
                var image = new FormData()
                image.append('file', this.$refs.file.files[0])

                $.ajax({
                    url: '/youxiang/api/upload/up',
                    data: image,
                    // headers: {
                    //     "Content-Type": "multipart/form-data"
                    // },
                    cache: false,
                    processData: false,
                    contentType: false,
                    type: 'post',
                    success: function(result) {
                        THIS.banner.bannerpic = result.url
                    },
                    error: function(error) {
                        alert('图片上传失败!');
                    }
                })
            }
        },
        add: function(){
            console.log(vm.bankCard)
            if(vm.bankCard){
                vm.showList = false;
                vm.title = "新增";
                vm.banner.bannerpic=null;
                vm.banner.bookname=null;
                vm.banner.id=null;
            }else {
                alert('请先于个人中心绑定银行卡')
            }

        },
        query: function () {
            vm.reload();
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }

            vm.showList = false;
            vm.title = "修改";
            vm.getBanner(id);

        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/banner/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        update2: function () {
            var url = "sys/banner/update2";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.category),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        getBanner: function(id){
            $.get(baseURL + "sys/banner/findById/"+id, function(r){
                vm.banner = r.banner;
            });
        },
        saveOrUpdate: function () {
            if(Number(vm.money)>=10){
                var sfile = new FormData()
                sfile.append('money', vm.money)
                sfile.append('code', vm.code);
                // sfile.append('isPay', _this.inputs[one]['checkbox']);
                sfile.append('phone', vm.bankCard.phone);
                $.ajax({
                    url: baseURL + '/sys/withdraw/apply',
                    data: sfile,
                    // headers: {
                    //"Content-Type": "multipart/form-$refs"
                    // "Content-Type": "text/plain;charset=UTF-8"
                    // },
                    cache: false,
                    processData: false,
                    contentType: false,
                    type: 'post',
                    success: function(r){
                        if(r.code==0){
                            vm.getBankCard()
                            alert('操作成功')
                            vm.showList = true;
                            vm.reload();
                        }else {
                            alert(r.msg)
                        }
                    }
                });
            }else {
                alert('可提现金额大于10元才能提现')
            }

        },
        cateName: function () {
            vm.showList = true;
            vm.showDom = 'list'
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'status': this.withdraw.status
                },
                page: page
            }).trigger("reloadGrid");
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page,
                postData:{'applyphone': vm.q.applyphone},
            }).trigger("reloadGrid");
        }
    }
});
